package edu.udel.mboch.towerdefense;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.HapticFeedbackConstants;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;

public class TowerDefenseAndroidActivity extends Activity implements OnClickListener
{
	private static final String STATE_PERSIST_FILENAME = "TowerDefenseReduxState";
	
	private TowerDefenseDatabase database;
	private TowerDefenseView TDView;
	private MainMenuView menu;
	private FrameLayout frame;
	private EditText input;
	
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
    	super.onCreate(savedInstanceState);
    	database = new TowerDefenseDatabase(this);
    }
    
    public void onResume()
    {
    	super.onResume();
    	
    	TDView = new TowerDefenseView(this, null, database);
    	menu = new MainMenuView(this, database);
    	TDView.setVisibility(View.INVISIBLE);
    	menu.setVisibility(View.VISIBLE);
    	frame = new FrameLayout(this);
    	frame.addView(TDView);
    	frame.addView(menu);
    	setContentView(frame);
    	
    	try
    	{
            FileInputStream fileIn = openFileInput(STATE_PERSIST_FILENAME);
            ObjectInputStream objectIn = new ObjectInputStream(fileIn);
            SinglePlayerGame game = (SinglePlayerGame)objectIn.readObject();
            objectIn.close();
            startNewGame(game);
        }
        catch (FileNotFoundException e)
        {
        }
        catch (Exception e)
        {
        }
    }
    
    public void onPause()
    {
    	super.onPause();
    	if(TDView.getVisibility() == View.VISIBLE)
    	{
    		try
    		{
    			FileOutputStream fileOut = openFileOutput(STATE_PERSIST_FILENAME, Context.MODE_PRIVATE);
    			ObjectOutputStream objectOut = new ObjectOutputStream(fileOut);
    			objectOut.writeObject(TDView.getGame());
    			objectOut.close();
    		}
    		catch (IOException e)
    		{
    		}
    	}
    	else
    		deleteFile(STATE_PERSIST_FILENAME);
    }
    
    private void newSinglePlayerGame(String name)
    {
    	final String nameFinal;
    	if(name.length() > 12)
    		nameFinal = name.substring(0,12);
    	else
    		nameFinal = name;
    	AlertDialog.Builder alert = new AlertDialog.Builder(this);
    	alert.setTitle("Please choose a difficulty");
    	CharSequence[] choices = new CharSequence[]{"Easy", "Medium", "Hard"};
    	alert.setSingleChoiceItems(choices, -1, new DialogInterface.OnClickListener()
    	{
    		public void onClick(DialogInterface dialog, int item)
    		{
    			if(item == 0)
    			{
    		    	SinglePlayerGame game = new SinglePlayerGame(new SinglePlayerTDState(Levels.loadFromStringRandomSP(Levels.EASY), new SingleHumanPlayer(nameFinal)), Levels.EASY);
    		    	startNewGame(game);
    		    	dialog.cancel();
    			}
    			else if(item == 1)
    			{
    				SinglePlayerGame game = new SinglePlayerGame(new SinglePlayerTDState(Levels.loadFromStringRandomSP(Levels.MEDIUM), new SingleHumanPlayer(nameFinal)), Levels.MEDIUM);
    		    	startNewGame(game);
    		    	dialog.cancel();
    			}
    			else if(item == 2)
    			{
    				SinglePlayerGame game = new SinglePlayerGame(new SinglePlayerTDState(Levels.loadFromStringRandomSP(Levels.HARD), new SingleHumanPlayer(nameFinal)), Levels.HARD);
    		    	startNewGame(game);
    		    	dialog.cancel();
    			}
    		}
    	});
    	alert.show();
    }
    
    private void newDemoGame()
    {
    	SinglePlayerGame game = new SinglePlayerGame(new SinglePlayerTDState(Levels.loadFromStringRandomSP(Levels.EASY), new SingleAIPlayer("Thor")), Levels.EASY);
    	startNewGame(game);
    }
    
    public void startNewGame()
    {
    	AlertDialog.Builder alert = new AlertDialog.Builder(this);
    	input = new EditText(this);
    	alert.setTitle("New Game");
        alert.setMessage("Please enter your name");
        alert.setView(input);
        alert.setPositiveButton("Enter", this);
        alert.show();
    }
    
    public void startNewGame(Game game)
    {
    	TDView.setGame(game);
    	TDView.setVisibility(View.VISIBLE);
    	menu.setVisibility(View.INVISIBLE);
    	TDView.bringToFront();
    	frame.invalidate();
    }
    
    public void openMenu()
    {
    	TDView.setVisibility(View.INVISIBLE);
    	menu.setVisibility(View.VISIBLE);
    	menu.bringToFront();
    	frame.invalidate();
    }
    
    public void onClick(DialogInterface dialog, int whichButton)
    {
    	newSinglePlayerGame(input.getText().toString());
    	dialog.cancel();
    }
    
    public boolean onCreateOptionsMenu(Menu menu)
    {
    	menu.add("Main Menu");
    	menu.add("Demo Mode");
    	menu.add("Exit");
    	return true;
    }
    
    public boolean onOptionsItemSelected(MenuItem item)
    {
    	CharSequence title = item.getTitle();
    	TDView.setHapticFeedbackEnabled(true);
        if (title.equals("Main Menu"))
        {
        	TDView.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
        	TDView.setVisibility(View.INVISIBLE);
        	menu.setMode(0);
        	menu.setVisibility(View.VISIBLE);
        	frame.invalidate();
        }
        else if(title.equals("Demo Mode"))
        {
        	TDView.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
        	newDemoGame();
        }
        else if (title.equals("Exit"))
        {
        	TDView.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
        	finish();
        }
        TDView.setHapticFeedbackEnabled(false);
        return true;
    }
}